package com.share.dgs.fetcher;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import com.netflix.graphql.dgs.InputArgument;
import com.share.dgs.model.Show;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caifan
 * @created 2021/3/5
 * description:
 */
@DgsComponent
public class ShowDatafetcher {
    @DgsData(parentType = "Query", field = "shows")
    public List<Show> shows(@InputArgument("title") String title, @InputArgument("releaseYear") Integer releaseYear) {
        if (title == null) {
            return shows;
        }

        return shows.stream().filter(s -> s.getTitle().contains(title)).collect(Collectors.toList());
    }


    // 模拟 DB 查询
/*    private final List<Show> shows = List.of(
            new Show("java", 1995),
            new Show("php", 1995),
            new Show("python", 1990),
            new Show("golang", 2009),
            new Show("rust", 2015)
    );*/
    private final List<Show> shows = Arrays.asList(
            new Show("java", 1995, new Date()),
            new Show("php", 1995, new Date()),
            new Show("python", 1990, new Date()),
            new Show("golang", 2009, new Date()),
            new Show("rust", 2015, new Date())
    );
}
